#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
	int t = 0, x = 0, len = 0, k = 0,i = 0,count1 = 0,count2 = 0,temp = 0;
	char arr[100005] = { 0 };
	scanf("%d", &t);
	getchar();
	while (t--)
	{
		gets(arr);
		scanf("%d", &k);
		getchar();
		len = strlen(arr);
		for (i = 0; i < len; i++)
		{
			if (arr[i] == '+')
			{
				count1++;
			}
			else
			{
				count2++;
			}
		}

		if (count1 > count2)
		{
			if (count1-count2 <= k)
			{
				while (count1 != count2)
				{
					count1--;
					count2++;
					k--;
				}
				if (k % 2 == 0)
				{
					x = 0;
					printf("%d\n", x);
				}
				else
				{
					x = 1;
					printf("%d\n", x);
				}
			}
			else
			{
				while (k-=2);
				{
					count1--;
					count2++;
				}
				x = x + count1 - count2;
				printf("%d\n", x);
			}
		}

		else if(count1<count2)
		{
			if (count2 - count1 <= k)
			{
				while (count1 != count2)
				{
					count2--;
					count1++;
					k-=2;
				}
				if (k % 2 == 0)
				{
					x = 0;
					printf("%d\n", x);
				}
				else
				{
					x = 1;
					printf("%d\n", x);
				}
			}
			else
			{
				while (k-=2);
				{
					count2--;
					count1++;
				}
				x = x + count2 - count1;
				printf("%d\n", x);
			}
		}
		else
		{
			if (k % 2 == 0)
			{
				x = 0;
				printf("%d\n", x);
			}
			else
			{
				x = 1;
				printf("%d\n", x);
			}
		}
		memset(arr, 0, 1);
		count1 = count2 = x = 0;
	}
	return 0;
}